<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="评价类型" prop="packageableType"-->
<!--                    v-if="(userStore.shopId == null && route.query.packageableType == null) || userStore.userType === '0'">-->
<!--        <el-select v-model="queryParams.orderableType" placeholder="请选择评价类型" clearable>-->
<!--          <el-option-->
<!--              v-for="dict in order_type"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="订单编号" prop="orderOn">
        <el-input
            v-model="queryParams.orderOn"
            placeholder="请输入订单编号"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名称"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="评价对象" prop="objectName">-->
<!--        <el-input-->
<!--            v-model="queryParams.objectName"-->
<!--            placeholder="请输入商家/活动名称"-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['pay:tyEvaluation:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['pay:tyEvaluation:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch"
                     @queryTable="getList(route.params && route.params.id)"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tyEvaluationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单编号" width="200" align="center" prop="orderOn"/>
      <el-table-column label="用户名称" width="100" align="center" prop="userName"/>
      <el-table-column label="评价对象" align="center" width="280" prop="objectName"/>
      <el-table-column label="类型" width="100" align="center" prop="orderableType">
        <template #default="scope">
          <dict-tag :options="order_type" :value="scope.row.orderableType"/>
        </template>
      </el-table-column>
      <el-table-column label="评价时间" width="180" align="center" prop="createTime"/>
      <el-table-column label="评价评论" align="center" width="300" prop="comment"/>
      <el-table-column label="评分" align="center" width="100" prop="rating"/>

      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row, scope.index)"
                     v-hasPermi="['pay:tyEvaluation:query']">详情
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['pay:tyEvaluation:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList(route.params && route.params.id)"
    />

    <el-dialog title="评价详情" v-model="open" width="700px" append-to-body>
      <el-form :model="evaluationForm" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="评价人：">{{ evaluationForm.userName }}</el-form-item>
            <el-form-item
                label="订单编号："
            >{{ evaluationForm.orderOn }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评价商品：">{{ evaluationForm.orderableName }}</el-form-item>
            <el-form-item label="评分：">{{ evaluationForm.rating }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容：">{{ evaluationForm.comment }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="附件：">
              <image-preview :src-list="evaluationForm.fileLists" :width="150" :height="150"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TyEvaluation">
import {
  listTyEvaluation,
  getTyEvaluation,
  delTyEvaluation,
  addTyEvaluation,
  updateTyEvaluation
} from "@/api/pay/tyEvaluation";
import useUserStore from '@/store/modules/user'

const userStore = useUserStore()
const route = useRoute();

const {proxy} = getCurrentInstance();

const tyEvaluationList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const {order_type} = proxy.useDict('order_type');

const data = reactive({
  form: {},
  evaluationForm: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    rating: undefined,
    comment: undefined,
    objectName: undefined,
    userName: undefined,
    orderNumber: undefined,
    orderableType: undefined
  },
  rules: {}
});

const {queryParams, form, rules, evaluationForm} = toRefs(data);

/** 查询订单评价列表 */
function getList(id) {
  if (id && typeof id !== 'object') {
    queryParams.value.orderableId = id;
    queryParams.value.orderableType = (route.query.orderableType != null) ? route.query.orderableType : queryParams.value.orderableType;
  }
  loading.value = true;
  listTyEvaluation(queryParams.value).then(response => {
    tyEvaluationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  queryParams.value.orderableType = null;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: undefined,
    userId: undefined,
    orderOn: undefined,
    orderableId: undefined,
    orderableType: undefined,
    rating: undefined,
    comment: undefined,
    createTime: undefined,
    updateTime: undefined,
    deleted: undefined
  };
  proxy.resetForm("tyEvaluationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList(route.params && route.params.id);
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加订单评价";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTyEvaluation(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改订单评价";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["tyEvaluationRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTyEvaluation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTyEvaluation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除订单评价编号为"' + _ids + '"的数据项？').then(function () {
    return delTyEvaluation(_ids);
  }).then(() => {
    getList(route.params && route.params.id);
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('pay/tyEvaluation/export', {
    ...queryParams.value
  }, `tyEvaluation_${new Date().getTime()}.xlsx`)
}

function handleView(row) {
  const _orderNo = row.orderOn
  getTyEvaluation(_orderNo).then(response => {
    evaluationForm.value = response.data;
    open.value = true;
  });
}

getList(route.params && route.params.id);
</script>
